﻿using Candy.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Candy
{
    public partial class LoginWindow : ChildWindow
    {
        public LoginWindow()
        {
            InitializeComponent();
            if (DataContext is LoginViewModel)
                (DataContext as LoginViewModel).PasswordAccessor = () => { return passwordBox.Password; };

  
#if DEBUG
            TxbxUser.Text = "jan";
            passwordBox.Password = "Warp10!";
#endif
        }


        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        public void UpdatePasswordSource()
        {
            var binding = passwordBox.GetBindingExpression(PasswordBox.PasswordProperty);
            if (binding != null)
                binding.UpdateSource();
        }

        public void UpdateUsernameSource()
        {
            var binding = TxbxUser.GetBindingExpression(TextBox.TextProperty);
            if (binding != null)
                binding.UpdateSource();
        }


        private void OnKeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                (DataContext as LoginViewModel).Login.Execute(null);
        }
    }
}

